package com.liull.netty.itcast.nio.bytebuffer;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Author: liull
 * @Description: 读写文件内容
 * @Date: Created in 20:35 2021/7/21
 * @Modified By:
 */
@Slf4j
public class ScatteringReadsTest {
    public static void main(String[] args) {
        try (RandomAccessFile raf = new RandomAccessFile("scattering.txt", "rw")) {
            FileChannel channel = raf.getChannel();
            ByteBuffer buffer1 = ByteBuffer.allocate(3);
            ByteBuffer buffer2 = ByteBuffer.allocate(3);
            ByteBuffer buffer3 = ByteBuffer.allocate(5);
            channel.read(new ByteBuffer[] {
                    buffer1, buffer2, buffer3
            });
            // 执行完上一句position 3 limit == capacity == 3
            buffer1.flip();
            buffer2.flip();
            buffer3.flip();
            ByteBufferUtil.printDesc(buffer1);
            ByteBufferUtil.printDesc(buffer2);
            ByteBufferUtil.printDesc(buffer3);
        } catch (IOException e) {

        }
    }
}
